package config

import (
	"common/helper"
	myredis "common/redis"
	"database/sql"
	"fmt"
)

// 抽奖配置
type LotteryCfg struct {
	IsEnabled         int    `json:"is_enabled" db:"is_enabled" cbor:"is_enabled"`
	OpenHour          int    `json:"open_hour" db:"open_hour" cbor:"open_hour"`
	Mode              int    `json:"mode" db:"mode" cbor:"mode"`
	DeductionRate     int    `json:"deduction_rate" db:"deduction_rate" cbor:"deduction_rate"`
	BaseAward         int    `json:"base_award" db:"base_award" cbor:"base_award"`
	MinRechargeAmount int    `json:"min_recharge_amount" db:"min_recharge_amount" cbor:"min_recharge_amount"`
	GreedyRate        int    `json:"greedy_rate" db:"greedy_rate" cbor:"greedy_rate"`
	DiscountRate      int    `json:"discount_rate" db:"discount_rate" cbor:"discount_rate"`
	H5Refresh         int    `json:"h5_refresh_interval_s" db:"h5_refresh_interval_s" cbor:"h5_refresh_interval_s"`
	RuleText          string `json:"rule_text" db:"rule_text" cbor:"rule_text"`
}

var G_LotteryCfg LotteryCfg

func LotteryCfgSetRow(rows *sql.Rows) {
	var err error
	err = rows.Scan(&G_LotteryCfg.OpenHour, &G_LotteryCfg.IsEnabled, &G_LotteryCfg.Mode, &G_LotteryCfg.DeductionRate,
		&G_LotteryCfg.BaseAward, &G_LotteryCfg.MinRechargeAmount, &G_LotteryCfg.GreedyRate, &G_LotteryCfg.DiscountRate,
		&G_LotteryCfg.H5Refresh, &G_LotteryCfg.RuleText)
	if err != nil {
		helper.InfoLog("LostReturnSetRow,scan err")
		return
	}
}

func LotteryCfgCfgRemove() {
}

func ReadLotteryCfgFromDB() {
	sqlSelect := fmt.Sprintf("select open_hour,is_enabled,mode,deduction_rate,base_award,min_recharge_amount," +
		"greedy_rate,discount_rate,h5_refresh_interval_s,rule_text from lottery_config")

	//sqlSelect := fmt.Sprintf("select is_enabled from lottery_config")
	err := myredis.GetMasterDBInstance().Get(&G_LotteryCfg, sqlSelect)

	if err != nil {
		helper.SqlSelectErrLog("ReadLotteryCfgFromDB:%s---%s", err.Error(), sqlSelect)
	}

	if G_LotteryCfg.MinRechargeAmount == 0 {
		G_LotteryCfg.MinRechargeAmount = 10
	}
}
